package com.itboye.textileexpres.login;

import java.util.Observable;
import java.util.Observer;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.textileexpes.beans.UserBean;
import com.itboye.textileexpes.presenter.GetYanzhengMaPresenter;
import com.itboye.textileexpes.presenter.MainPresenter;
import com.itboye.textileexpres.R;
import com.itboye.textileexpres.base.BaseActivity;
import com.itboye.textileexpres.base.ResultEntity;
import com.itboye.textileexpres.base.SpPublicParams;
import com.itboye.textileexpres.logincontroller.LoginController;
import com.itboye.textileexpres.logincontroller.LoginedState;
import com.itboye.textileexpres.util.SPUtils;
import com.itboye.textileexpres.util.TimeCount;
import com.itboye.textileexptes.config.GlobalConfig;

/**
 * 
 * @author 验证码登录获取验证码
 * 
 */
public class SMSReceiveActivity extends BaseActivity implements
		OnClickListener, Observer {
	ImageView back;
	TextView tvphone;
	Button tvTime;
	String phone;
	String time;
	TimeCount count;
	String code;
	String number;
	EditText edittextVD;
	TextView next_tv, next_relogin;
	GetYanzhengMaPresenter getYanzhengMaPresenter;
	Intent intent;
	String type;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_sms_receive;
	}

	@Override
	public void onMyCreate() {
		// TODO Auto-generated method stub
		phone = getIntent().getStringExtra("phone");
		number = getIntent().getStringExtra("number");
		type = getIntent().getStringExtra("type");
		code = getIntent().getStringExtra("code");
		edittextVD.setText(code);
		count = new TimeCount(60000, 1000, tvTime);
		if (type.equals("register")) {
			next_relogin.setVisibility(View.VISIBLE);
			next_tv.setVisibility(View.GONE);
		} else
		// if (type.equals("2"))
		{
			next_relogin.setVisibility(View.GONE);
			next_tv.setVisibility(View.VISIBLE);
		}
		count.start();
		tvphone.setText(number + phone);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back:
			finish();
			break;
		case R.id.tvTime:
			if (type.equals("register")) {
				getYanzhengMaPresenter = new GetYanzhengMaPresenter(this);
				getYanzhengMaPresenter.getVerificationCode(number, phone,
						1 + "");
			} else
			// if (type.equals("2"))
			{
				getYanzhengMaPresenter = new GetYanzhengMaPresenter(this);
				getYanzhengMaPresenter.getVerificationCode(number, phone,
						2 + "");
			}
			count.start();
			break;
		case R.id.next_tv:
			if (code != null) {
				next();
			}
			break;
		case R.id.next_relogin:
			if (code != null) {
				next();
			}
			break;

		default:
			break;
		}
	}

	void next() {
		intent = new Intent(this, LoginReginPwasswroldActibity.class);
		intent.putExtra("phone", phone);
		intent.putExtra("number", number);
		intent.putExtra("type", type);
		intent.putExtra("code", edittextVD.getText().toString());
		startActivity(intent);
		finish();
	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub

		ResultEntity entity = super.handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == GetYanzhengMaPresenter.YanzhengmaSuccessEvent) {
				String codeString = entity.getData().toString();
				String VD = codeString.substring(codeString.length() - 8,
						codeString.length() - 2);
				toast(codeString);
				code = VD;
				edittextVD.setText(code);
			} else if (entity.getEventType() == GetYanzhengMaPresenter.YanzhengmaFailEvent) {
				toast(entity.getData() + "");
			}

		}
	}

}
